//
//  HYLStatusRetweetView.swift
//  iOSwechat
//
//  Created by apple on 16/3/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class HYLStatusRetweetView: UIView {
    
    // MARK: - 属性
    private var statusrRetweetViewBottom: Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 被转发的微博数据
    var status: HYLStatus? {
        didSet{
            let name = status?.user?.screen_name ?? "没有名称"
            let text = status?.text ?? "没有内容"
            retweetContentLabel.text = "\(name) : \(text)"
            
            
            statusrRetweetViewBottom?.install()
            if let _ = status?.storePictureURLs {
                // 如果有图片
                retweetPictureView.hidden = false
                // 设置配图显示内容
                retweetPictureView.status = status
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusrRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(StatusCellMargin).constraint
                })
            } else {
                retweetPictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusrRetweetViewBottom = make.bottom.equalTo(retweetContentLabel).offset(StatusCellMargin).constraint
                })
            }
            
        }
    }


    
    func prepareUI() {

        // 添加控件
        // 整块大背景
        addSubview(retweetBkgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 设置按钮
        // 整块大背景约束
        retweetBkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 被转发内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(StatusCellMargin)
            make.trailing.equalTo(self).offset(-StatusCellMargin)
        }
        
        // 被转发图片
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StatusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        

        // HYLStatusRetweetView 自己高度更加控件内容
        self.snp_makeConstraints { (make) -> Void in
            self.statusrRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(StatusCellMargin).constraint
        }
    }
    
    
    
    // MARK: - 懒加载
    // 整块大背景约束
    private lazy var retweetBkgButton: UIButton = {
       let bgBotton = UIButton()

        bgBotton.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        bgBotton.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return bgBotton
    }()
    
//
    
    // 被转发的内容
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        label.numberOfLines = 0
        return label
    }()
    
    
    // 被转发的微博配图
    private lazy var retweetPictureView: HYLStatusPictureView = HYLStatusPictureView()

}



